<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .carousel-container {
            width: 500px;
            height: 300px;
            margin: 0 auto;
            overflow: hidden;
            position: relative;
        }

        .arrow-left,
        .arrow-right {
            width: 50px;
            height: 300px;
            text-align: center;
            line-height: 300px;
            background-color: rgba(0, 0, 0, 0.3);
            color: #fff;
            font-size: 30px;
            position: absolute;
            top: 0;
            opacity: 0;
            transition: all 0.1s linear;
        }

        .arrow-left {
            left: 0;
        }

        .arrow-right {
            right: 0;
        }

        .arrow-left:hover,
        .arrow-right:hover {
            opacity: 1;
        }

        .carousel-list {
            /* width: 3000px; */
            height: 300px;

        }

        .carousel-list img {
            width: 500px;
            height: 300px;
            float: left;
        }

        .btns {
            width: 100%;
            height: 50px;
            position: absolute;
            bottom: 0px;
            text-align: center;
        }

        .btns span {
            display: inline-block;
            width: 10px;
            height: 2px;
            background-color: #fff;
            margin: 4px;
        }

        .btns .active {
            background-color: #ffa44f;
        }
    </style>
</head>

<body>
    <div class="carousel-container">
        <div class="arrow-left">&lt;</div>
        <div class="carousel-list">

        </div>
        <div class="arrow-right">&gt;</div>
        <div class="btns">
        </div>
    </div>
    <script src="./animate.js"></script>
    <script>
        (function () {
            function $(selector) {
                return document.querySelector(selector);
            }
            function $$(selector) {
                return document.querySelectorAll(selector);
            }
            var doms = {
                container: $('.carousel-container'),
                imgBox: $('.carousel-list'),
                arrowLeft: $('.arrow-left'),
                arrowRight: $('.arrow-right'),
                btnBox: $('.btns'),
            };
            var urls = [
                './img/Wallpaper1.jpg',
                './img/Wallpaper2.jpg',
                './img/Wallpaper3.jpg',
                './img/Wallpaper4.jpg',
                './img/Wallpaper5.jpg',
            ];
            var containerWidth = doms.container.clientWidth; // 容器可见区域的宽度
            var curIndex = 0;// 当前选中的图

            function init() {
                // 初始化
                function _createImg(url) {
                    var img = document.createElement('img');
                    img.src = url;
                    doms.imgBox.appendChild(img);
                }

                for (var i = 0; i < urls.length; i++) {
                    _createImg(urls[i]);
                    // 创建指示器
                    var div = document.createElement('span');
                    doms.btnBox.appendChild(div);
                }

                // 多加一张额外的图片
                _createImg(urls[0]);

                // 设置容器宽度
                doms.imgBox.style.width = doms.imgBox.children.length + '00%';

                // 激活状态
                setIndicatorStatus();

                // 绑定事件
                initEvents();

            }
            /*根据curIndex设置指示器的状态*/
            function setIndicatorStatus() {
                // 1. 将目前激活的指示器取消激活
                var active = doms.btnBox.querySelector('.active');
                if (active) {
                    active.className = '';
                }
                // 2. 激活当前的指示器
                var index = curIndex;
                if (index > urls.length - 1) {
                    index = 0;
                }
                doms.btnBox.children[index].className = 'active';
            }

            function initEvents() {

                // 1. 左右点击事件
                doms.arrowLeft.onclick = prev;
                doms.arrowRight.onclick = next;
                // 2. 处理指示器的点击事件
                for (var i = 0; i < doms.btnBox.children.length; i++) {
                    (function (i) {
                        doms.btnBox.children[i].onclick = function () {
                            moveTo(i);
                        };
                    })(i);
                }
                // 3. 定时器
                var duration = 2000; // 自动切换的间隔
                var timerId;
                function autoStart() {
                    if (timerId) {
                        // 已经有自动切换在进行了
                        return;
                    }
                    timerId = setInterval(next, duration);
                }

                function autoStop() {
                    clearInterval(timerId);
                    timerId = null;
                }

                autoStart(); // 最开始自动切换
                doms.container.onmouseenter = autoStop;
                doms.container.onmouseleave = autoStart;
            }
            // 下一张
            var next = function () {
                var newIndex = curIndex + 1;
                var onend;
                if (newIndex === doms.imgBox.children.length - 1) {
                    // 切换到最后一张图片了
                    // 等动画完成后，要回到第一张图片
                    onend = function () {
                        doms.imgBox.style.marginLeft = 0;
                        curIndex = 0;
                    };
                }
                moveTo(newIndex, onend);
            }
            // 上一张
            var prev = function () {
                var newIndex = curIndex - 1;
                if (newIndex < 0) {
                    var maxIndex = doms.imgBox.children.length - 1;
                    doms.imgBox.style.marginLeft = -maxIndex * containerWidth + 'px';
                    newIndex = maxIndex - 1;
                }
                moveTo(newIndex);
            }
            // 交互
            var totalMS = 300;
            var isPlaying = false; // 是否有正在进行的切换动画
            /**
             * 将轮播图从当前的位置，切换到newIndex位置
             * @param {*} newIndex 新的位置的图片索引
             */
            var moveTo = function (newIndex, onend) {
                if (isPlaying || newIndex === curIndex) {
                    return; // 有动画进行 或 切换目标和当前一致，不做任何事情
                }
                isPlaying = true;
                var from = parseFloat(doms.imgBox.style.marginLeft) || 0;
                var to = -newIndex * containerWidth;

                createAnimation({
                    from: from,
                    to: to,
                    totalMS: totalMS,
                    onmove: function (n) {
                        doms.imgBox.style.marginLeft = n + 'px';
                    },
                    onend: function () {
                        isPlaying = false;
                        onend && onend();
                    },
                });

                curIndex = newIndex; // 更改当前显示的图片索引
                setIndicatorStatus();
            }
            init();
        })()
    </script>
</body>

</html>